package com.asw.springboot.controller;


import com.asw.springboot.common.CommonController;
import com.asw.springboot.common.exception.AdminException;
import com.asw.springboot.common.exception.ApiException;
import com.asw.springboot.common.fastdfs.FastDfsUtil;
import com.asw.springboot.common.redis.IGlobalRedisService;
import com.asw.springboot.common.result.GlobalErrorInfoEnum;
import com.asw.springboot.common.result.ResultBody;
import com.asw.springboot.po.City;
import com.asw.springboot.service.ICityService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by hxq on 2017/5/11.
 */
@RestController
public class HelloWorldController extends CommonController{

    @Autowired
    private ICityService cityService;

    @Autowired
    RedisTemplate <String,String> redisTemplate;

    @RequestMapping(value = "/say")
    public ResultBody sayHello(@RequestParam(value = "files", required = false) MultipartFile[] multipartFile){
        List<String> stringList = FastDfsUtil.fastUploadFileArray(multipartFile);
        return new ResultBody(stringList);
    }


    @RequestMapping(value = "/modify/{id}",method = RequestMethod.POST)
    public ResultBody modifyCity(@PathVariable("id") Integer id){
        return null;
    }


    /***
     * 根据异常的信息来抛出到具体的页面或者是json串，如果系统想异常和都用json来接受，就用此方法，反之用下面的返回错误页面
     * @param cityName
     * @return
     * @throws ApiException
     */
    @RequestMapping(value = "/findCityById")
    public ResultBody findCityById(String cityName)throws ApiException{
        if (StringUtils.isEmpty(cityName)){
            return new ResultBody(GlobalErrorInfoEnum.PARAMS_NO_COMPLETE);
        }
        try{
            PageHelper.startPage(getPageNum(),getPageSize());
            List<City> cityList = cityService.findAllCity();
            City city = cityList.get(2);
            return new ResultBody(city);
        }catch (Exception e){
            throw new ApiException(e);
        }
    }


    @RequestMapping(value = "/findCity")
    public ResultBody findCity(String cityName)throws AdminException{
        if (StringUtils.isEmpty(cityName)){
            return new ResultBody(GlobalErrorInfoEnum.PARAMS_NO_COMPLETE);
        }
        try{
            PageHelper.startPage(getPageNum(),getPageSize());
            List<City> cityList = cityService.findAllCity();
            City city = cityList.get(2);
            return new ResultBody(city);
        }catch (Exception e){
            throw new AdminException(e);
        }
    }
}
